﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//旋转过后，mid的左右两边，一定有一边是有序的
//有序区间的特征是，最右边的大于最左边的值


public class Solution33
{
    public int Search(int[] nums, int target)
    {
        int left = 0;
        int right = nums.Length - 1;
        int mid=(left + right+1)/2;
        while (left <= right)
        {
            if (target == nums[mid])
            {
                return mid; }


                if (nums[mid] <= nums[right])//右半边有序,记得加等号=
            {

                if (target < nums[mid])//一定去左半边
                {
                    right = mid - 1;
                }
                else//target大于中间，可能右，也可能大于最右边，变左边
                {
                    if(target > nums[right])//超出右边界
                    {
                        right = mid - 1;
                    }
                    else//等于右边界或者小于右边界，都在右边
                    {
                        left = mid + 1;
                    }
                     

                    
                }

            }
            else//左半边有序
            {
                if (target > nums[mid])//一定去右半边
                {
                    left = mid + 1;
                }
                //23567012
                else//target小于中间，可能左，也可能小于最左边，变右边
                {

                    if(target < nums[left])
                    {
                        left = mid + 1;
                    }
                    else
                    {

                        right = mid - 1;
                    }



                }
            }


            Console.WriteLine("mid:   "+mid+ "   nums[mid]:"+ nums[mid]);

           
            
           

            mid = (left + right + 1) / 2;

        }


        return -1;
    }
}
class Program33
{
    static void Main33(string[] args)
    {
        Solution33 solution = new Solution33();
        //int[] nums = [4, 5, 6, 7, 0, 1, 2];
        int[] nums = [3,1];
        int target = 3;
      int res=  solution.Search(nums, target);
        Console.WriteLine(res);
    }
}
